package com.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.jpa.UserRepository;
import com.mapper.UserMapper;
import com.model.User;

@Controller
public class UserController {
	
	@Resource
	UserMapper userMapper;
	
	@Resource
	private UserRepository userRepository;
	
	
	@CrossOrigin(origins = "http://127.0.0.1:8020")
	@RequestMapping(value="user/add10",method=RequestMethod.GET)
	public @ResponseBody String add10() {
		for (int i = 0; i < 10; i++) {
			userRepository.save(new User("qky"+i, "好无聊啊不知道做什么啊啊啊啊啊啊"+i, "唐山", i*2, i*3, i*4));
		}
		return "添加成功";
	}
	
	
	@RequestMapping(value="user/delelteAll",method=RequestMethod.GET)
	public @ResponseBody String delelteAll() {
		userRepository.deleteAll();
		return "删除成功";
	}
	
	@RequestMapping(value="user/add",method=RequestMethod.POST)
	public @ResponseBody String add(@RequestBody User user) {
		System.out.println(user);
		user.setAddress("唐山");
		user.setShuoshuo("不知道做什么好无聊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊");
		userRepository.save(user);
		return "添加成功";
	}
	
	
	
	
	@CrossOrigin(origins = "http://127.0.0.1:8020")
	@RequestMapping(value="user/show",method=RequestMethod.POST)
	public @ResponseBody List<User> show() {
		System.out.println("进入show");
		List<User> list=userRepository.findAll();
		return list;
	}
	
	@RequestMapping("add/{name}")
	public @ResponseBody String add(@PathVariable String name) {
		userRepository.deleteAll();
		System.out.println(name);
		//userMapper.insert(name);
		for (int i = 0; i < 10; i++) {
			userRepository.save(new User(name+i));
		}
		return "添加成功";
	}
	
	@RequestMapping("show")
	public String show(ModelMap map) {
		List<User> list=userRepository.findAll();
		map.put("list", list);
		return "show";
	}
	
	@RequestMapping("show2")
	public String show2() {
		return "show2";
	}
	
	@RequestMapping("show3")
	public @ResponseBody List<User> show3() {
		System.out.println("进入show3");
		List<User> list=userRepository.findAll();
		return list;
	}
	
	@RequestMapping("show4")
	public @ResponseBody String show4() {
		System.out.println("进入show4");
		return "成功";
	}
	
	@RequestMapping("/")
    public String index(ModelMap map) {
        // 加入一个属性，用来在模板中读取
        map.addAttribute("host", "http://blog.didispace.com");
        // return模板文件的名称，对应src/main/resources/templates/index.html
        return "index";  
    }
}
